<?php
    class admin_addProduct_controller extends controllerBase
    {
        public function process()
        {
            $oDatabase = new database();
            $aSettings = database::getListSettings('add_product');
            $this->aParams['aSettings'] = $aSettings;
            if(isset($_POST['is_add_product']))
            {
                $sProductId = DBObject::addProduct($aSettings['default_user_add_product']['setting_key'],$_POST['tensp'],$_POST['dongia'],$_POST['description'],$_POST['avatar'],$_POST['category_uri'],$_POST['list_categories'],$_POST['dm_name'],$_POST['location_id']);
                $this->aParams['new_product_uri'] = $sProductId;
                $aData = array(
                    'url' => trim($_POST['url']),
                    'status' => 1,
                );

                $oDatabase->add('product_list_url',$aData);
            }
            else
            {
                if(isset($_POST['url']))
                {
                    $sQuery = "SELECT * FROM product_list_url WHERE url ='".trim($_POST['url'])."'";
                    $result = $oDatabase->getObject($sQuery);
                    if($result)
                    {
                        $this->aParams['bIsExist'] = true;
                    }
                    else
                    {
                        $sQuery = "SELECT * FROM provider WHERE id='".$_POST['tb_provider']."'";
                        $aProvider = $oDatabase->getObject($sQuery);
                        $this->aParams['oProvider'] = $aProvider;
                        // Create DOM from URL
                        $html = file_get_html($_POST['url']);

                        // Find all article blocks
                        foreach($html->find($aProvider->title_sector) as $article) {
                            $this->aParams['title'] = $article->plaintext;
                        }

                        foreach($html->find($aProvider->price_sector) as $article) {
                            $this->aParams['price'] = preg_replace('/\D/', '', $article->plaintext);
                        }

                        foreach($html->find($aProvider->description_sector) as $article) {
                            $this->aParams['description'] =  $article->plaintext;                  
                        }

                        $i = 0;
                        $aImages = array();                       
                        foreach($html->find($aProvider->image_sector) as $article) {
                            $sFilenameIn  = trim($article->src);
                            $sFilenameIn = URL::convertLink($sFilenameIn);
                            $sFileName = end(explode('/',$sFilenameIn));
                            $sFilenameOut = 'upload/'.$sFileName;

                            $contentOrFalseOnFailure   = file_get_contents($sFilenameIn);
                            $byteCountOrFalseOnFailure = file_put_contents($sFilenameOut, $contentOrFalseOnFailure);

                            $aImages[] = $sFileName;
                            $i+=1;
                            if($i >= $aSettings['default_number_image_download']['setting_value'])
                                break;
                        }
                        $this->aParams['aImages'] = $aImages;
                    }
                }
            }
        }
    }
?>
